package agents;


import jade.core.AID;
import jade.domain.DFService;
import jade.domain.FIPAException;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import jade.gui.GuiAgent;
import jade.gui.GuiEvent;
import jade.lang.acl.ACLMessage;

import java.io.IOException;
import java.util.Vector;

import model.Position;

public class PowerAgent extends GuiAgent {
	private static final long serialVersionUID = 1L;

	private AID factory;
	private Position position;
	private Vector<AID> agvs = new Vector<AID>();


	public PowerAgent(Position position) {
		this.position = position;
	}

	protected void setup() {
		System.out.println(getLocalName() + ": Hello World!");

		registerOnDF();

		getFactory();

		getAgvList();
		
		informAGVs();

	}

	private void informAGVs() {
		ACLMessage msg = new ACLMessage(ACLMessage.INFORM);
		try {
			msg.setContentObject(position);
		} catch (IOException e) {
			e.printStackTrace();
		}

		for (int i = 0; i < agvs.size(); ++i) {
			msg.addReceiver(agvs.get(i));
		}
		send(msg);
	}

	private void getAgvList() {
		try {
			DFAgentDescription template = new DFAgentDescription();
			ServiceDescription templateSd = new ServiceDescription();
			templateSd.setType("agv");
			template.addServices(templateSd);

			DFAgentDescription[] results = DFService.search(this, template);
			if (results.length > 0) {
				System.out.println("Agent " + getLocalName()
						+ " found the following agv services:");
				for (int i = 0; i < results.length; ++i) {
					DFAgentDescription dfd = results[i];
					AID provider = dfd.getName();
					agvs.add(provider);
					System.out.println("\t - " + provider.getLocalName());
				}
			} else {
				System.out.println("Agent " + getLocalName()
						+ " did not find any agv service");
			}
		} catch (FIPAException fe) {
			fe.printStackTrace();
		}
	}
	private void getFactory() {
		try {
			DFAgentDescription template = new DFAgentDescription();
			ServiceDescription templateSd = new ServiceDescription();
			templateSd.setType("factory");
			template.addServices(templateSd);

			DFAgentDescription[] results = DFService.search(this, template);
			if (results.length > 0) {
				System.out.println("Agent " + getLocalName()
						+ " found the following factory services:");
				for (int i = 0; i < results.length; ++i) {
					DFAgentDescription dfd = results[i];
					AID provider = dfd.getName();
					factory = provider;
					// agvs.add(provider);
					System.out.println("\t - " + provider.getLocalName());

					// //The same agent may provide several services; we are
					// only interested
					// Iterator it = dfd.getAllServices();
					// while (it.hasNext()) {
					// ServiceDescription sd = (ServiceDescription) it.next();
					// if (sd.getType().equals("weather-forecast")) {
					// System.out.println("- Service \""+sd.getName()+"\" provided by agent "+provider.getName());
					// }
					// }
				}
			} else {
				System.out.println("Agent " + getLocalName()
						+ " did not find any factory service");
			}
		} catch (FIPAException fe) {
			fe.printStackTrace();
		}
	}

	private void registerOnDF() {
		// registar no DF
		DFAgentDescription dfd = new DFAgentDescription();

		ServiceDescription sd1 = new ServiceDescription();
		sd1.setName(getName());
		sd1.setType("power");

		dfd.setName(getAID());
		dfd.addServices(sd1);

		try {
			DFService.register(this, dfd);
		} catch (FIPAException e) {
			System.out.println(getLocalName() + "Cannot register with DF");
			doDelete();
		}

	}

	@Override
	protected void onGuiEvent(GuiEvent arg0) {

	}

	public Position getPosition() {
		return position;
	}
}
